package cn.edu.gdut.deepcode.config;

import cn.edu.gdut.deepcode.interceptor.LoginInterceptor;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * <p>
 * web相关配置类
 * </p>
 *
 * @Author: 暮倦
 * @Since: 2025-04-08  21:04
 * @Version: 1.0.0
 */
@Component
@RequiredArgsConstructor
public class MvcConfig implements WebMvcConfigurer {

    private final LoginInterceptor loginInterceptor;

    private final  AuthProperties authProperties;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        System.out.println(authProperties.getExcludePaths());
        registry.addInterceptor(loginInterceptor)
                .excludePathPatterns(authProperties.getExcludePaths())
                .addPathPatterns(authProperties.getIncludePaths());
    }

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOrigins("http://localhost:3000")
                .allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS")
                .allowedHeaders("*")
                .allowCredentials(true)
                .exposedHeaders("*");
    }
}
